//
//  Card.m
//  CardSwitchDemo
//
//  Created by Apple on 2016/11/9.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "XLCard.h"
#import "XLCardItem.h"
#import "SINAUiviewClickLiserner.h"

@interface XLCard () {
    UIImageView *_imageView;
    //点击事件
    UIView *_otherClickView;
    UILabel *_textLabel;
    //在线人数
    UILabel *_peopLabel;
}
@end

@implementation XLCard

- (instancetype)initWithFrame:(CGRect)frame {
    
    return [self initWithFrame:frame :YES];
}
- (instancetype)initWithFrame:(CGRect)frame :(BOOL)isShowTitle{
    if (self = [super initWithFrame:frame]) {
        [self buildUI:isShowTitle];
    }
    return self;
}
- (void)buildUI :(BOOL)isShowTitle{
    self.layer.cornerRadius = 10.0f;
    self.layer.masksToBounds = true;
    self.backgroundColor = [UIColor whiteColor];
    
    CGFloat labelHeight = self.bounds.size.height * 0.20f;
    
     CGFloat imageViewHeight = self.bounds.size.height - labelHeight;
    
    if (self.isShowTitle) {
         imageViewHeight = self.bounds.size.height - labelHeight;
    } else {
         imageViewHeight = self.bounds.size.height;
    }
   
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, imageViewHeight)];
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    _imageView.layer.masksToBounds = true;
    [self addSubview:_imageView];
    
    _otherClickView=[[UIView alloc]initWithFrame:CGRectMake(20, 20, self.bounds.size.width-40, self.bounds.size.height-40)];
    _otherClickView.backgroundColor=[UIColor clearColor];
    [self addSubview:_otherClickView];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:_otherClickView andWithBlick:^(UIView *view) {
        if (self.clickBlock) {
            self.clickBlock(_otherClickView);
        }
    }];
    
    
    _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, imageViewHeight, self.bounds.size.width, labelHeight)];
    _textLabel.textColor = [UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1];
    _textLabel.font = [UIFont systemFontOfSize:22];
    _textLabel.textAlignment = NSTextAlignmentCenter;
    _textLabel.adjustsFontSizeToFitWidth = true;
    if (self.isShowTitle) {
        [self addSubview:_textLabel];
    }
    
    _peopLabel=[[UILabel alloc]initWithFrame:CGRectMake(self.bounds.size.width-215, self.bounds.size.height-40,200,40)];
    _peopLabel.font = [UIFont systemFontOfSize:14];
    _peopLabel.textAlignment = NSTextAlignmentRight;
    _peopLabel.adjustsFontSizeToFitWidth = true;
     _peopLabel.textColor = [UIColor whiteColor];
    _peopLabel.text=@"200人在线";
    [self addSubview:_peopLabel];
    
}

-(void)setItem:(XLCardItem *)item {
    _imageView.image = [UIImage imageNamed:item.imageName];
    _textLabel.text = item.title;
    _peopLabel.text=[NSString stringWithFormat:@"%@人在线",item.personNumber];
}

@end
